package com.leyou.item.controller;


import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.entity.TbSpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecService specService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<TbSpecGroup>> querySepcGroupsWithParamsByCid(@PathVariable("cid") Long cid){
        List<TbSpecGroup> tbSpecGroups = specService.querySepcGroupsWithParamsByCid(cid);
        if(tbSpecGroups==null || tbSpecGroups.size()<1){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(tbSpecGroups);
    }

    @PostMapping("group")
    public ResponseEntity<Void> addGroup( TbSpecGroup group){
        try {
            specService.addSpecGroup(group);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }

    }


    @PutMapping("group")
    public ResponseEntity<Void> updateGroup( TbSpecGroup group){
        try {
            specService.updateSpecGroup(group);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }

    }
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("id") Long id){
        try {
            specService.deleteSpecGroup(id);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }

    }


    /*
    * 操作参数表信息
    *
    * */
    //查询
    @GetMapping("params")
    public ResponseEntity<List<TbSpecParam>> queryParams(
            Long gid,
            Long cid,
            Boolean searching,
            Boolean generic
    ){
        try {
            List<TbSpecParam> tbSpecParams= specService.queryParams(gid,cid,searching,generic);
            return ResponseEntity.ok(tbSpecParams);
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

    }


    //添加
    @PostMapping("param")
    public ResponseEntity<Void> addParam(@RequestBody TbSpecParam param){
        try {
            specService.addParam(param);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }


    //修改
    @PutMapping("param")
    public ResponseEntity<Void> updateParam( @RequestBody TbSpecParam param){
        try {
            specService.updateParam(param);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }
    //删除
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> addParam(@PathVariable("id") Long id){
        try {
            specService.deleteParam(id);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }
}
